package com.smalls.newvideotwo.mvp.model;

import android.os.Handler;

import com.smalls.newvideotwo.Constants;
import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.bean.MovieDetail;
import com.smalls.newvideotwo.mvp.beanResult.GetHotMovieResult;
import com.smalls.newvideotwo.mvp.http.CommonParamsInterceptor;
import com.smalls.newvideotwo.mvp.http.Result;
import com.smalls.newvideotwo.mvp.model.modelInterface.IGetMovieDetail;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.util.UIHelper;
import com.smalls.newvideotwo.utils.Constant;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by hs on 2018/3/20.
 */

public class GetMovieDetailModel implements IGetMovieDetail {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetMovieDetailModel":"GetMovieDetailModel";
    Handler handler = new Handler();
    private Call call;
    public void Abort() {
        handler.removeCallbacksAndMessages(null);
        if(call!=null&&!call.isCanceled()) {
            call.cancel();
        }
    }
    @Override
    public void getMovieDetail(String movie_id, String category_id,final BaseBeanResult baseBeanResult) {
        LogUtil.d(this.TAG, "getHotMovie");
        final Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("Authorization","Bearer "+ NewDTVApp.getApp().getToken())
                .url(Constants.API_DETAIL())
                .get()
                .build();

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(NewDTVApp.getApp().loggingInterceptor)
                .addInterceptor(new CommonParamsInterceptor.Builder()
                        .addQueryParam("movie_id", "eq."+movie_id)
                        .addQueryParam("category_id", "eq."+category_id)
                        .build())
                .build();
        try {
        call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, final IOException e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                baseBeanResult.onError(e);
                                //UIHelper.toastServerConnectErr();
                            }
                        });

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String resultStr = response.body().string();
                        if(response.code()!=200) {
                            baseBeanResult.onError(new Exception());
                            return;
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    if(resultStr.equals("Bad Request")) {
                                        baseBeanResult.onNext(null);
                                    } else {
                                        String addData = "{data:" + resultStr + "}";
                                        GetHotMovieResult result = Result.getResult(addData, GetHotMovieResult.class);
                                        if (result != null
                                                && result.getData() != null
                                                && result.getData().size() > 0) {
                                            MovieDetail movieDetail = result.getData().get(0);
                                            baseBeanResult.onNext(movieDetail);
                                            return;
                                        }
                                    }
                                    /// toastServerResponseErr(result.errorMessage);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    baseBeanResult.onError(e);
                                    UIHelper.toastServerResponseErr("something wrong with data.");
                                }
                            }
                        });

                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
            baseBeanResult.onError(e);
            //UIHelper.toastServerResponseErr("something wrong with data.");
        }
    }
}
